//
//  MyCollectionViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/24.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "MyCollectionModel.h"
#import "MyCollectionTableViewCell.h"
#import "TuisuanInfoViewController.h"

@interface MyCollectionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <MyCollectionModel *> * modelArray;

@end

@implementation MyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [MyCollectionModel myCollectionModelWithSucess:^(NSArray<MyCollectionModel *> *array) {
      
        self.modelArray = array;
    } error:^{
        
    }];
    
    [self setupNav];
    [self setupUI];
}

- (void)setModelArray:(NSArray<MyCollectionModel *> *)modelArray{
    
    _modelArray = modelArray;
    
    [self.tableView reloadData];
}

- (void)setupNav{
    
    self.title = @"我的评论";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 80 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyCollectionTableViewCell class] forCellReuseIdentifier:@"MyCollectionListCell"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCollectionListCell" forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SHOWHUD;
    
    MyCollectionModel * model = self.modelArray[indexPath.row];
    
    NSString * str = [NSString stringWithFormat:@"/index/Liuhetreasure/pairDetails?id=%@",model.ID];
    
    [[NetworkTool sharedTools] GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        HIDDENHUD;
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            NSDictionary * data = responseObject[@"data"];
            
            HIDDEN_TAB;
            
            TuisuanInfoViewController * infoVC = [TuisuanInfoViewController new];
            
            infoVC.titleStr = @"收藏详情";
            infoVC.content = data[@"content"];
            infoVC.from = @"myself";
            
            [self.navigationController pushViewController:infoVC animated:YES];
 
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        HIDDENHUD;
    }];
}


- (void)backToView {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
